<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1200px" append-to-body>
        <el-row type="flex">
            <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download"  :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
        </el-row>
        <el-row type="flex">
            <el-col :span="24">
                <G2BarChart idName="spuInfoChat" :chartData="spuData" :height="300" label="count" />
            </el-col>
        </el-row>
        <el-row type="flex" style="height: 300px; margin-top: 16px;">
            <el-col :span="24">
                <ColorChart idName="colorChat" :chartData="colorData" label="count" />
            </el-col>
            <el-col :span="24">
                <CupChart idName="cupChat" :chartData="cupData" label="count" />
            </el-col>
        </el-row>
    </el-dialog>
</template>

<script>
import { queryStockInfo,exportNeedInfo } from '@/api/report'
import { exportFile } from '@/utils/index'
import G2BarChart from './G2BarChart'
import ColorChart from './ColorChart'
import CupChart from './CupChart.vue'

export default {
    name: 'SkuListDialog',
    components: {
        G2BarChart,
        ColorChart,
        CupChart,
    },
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            spuData: [],
            colorData: [],
            cupData: [],
            rules: {},
            download: false,
        }
    },
    computed: {
        title() {
            return `sku缺货`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },

    mounted() {
        this.getDetail()
    },
    methods: {
        handleExport() {
            this.download = true
            const params = { spu: this.params.spu}
            exportNeedInfo(params)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        getDetail() {
            this.loading = true
            const params = { spu: this.params.spu}
            queryStockInfo(params)
                .then(res => {
                    this.spuData =
                        res?.data?.spu?.map(item => {
                            item.count = parseFloat(item.count)
                            return { ...item }
                        }) ?? []
                    this.colorData = res.data?.color ?? []
                    this.cupData = res?.data?.cup ?? []
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                supplyEdit(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style></style>
